package com.learning.algorithm.util;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author: daqian_liao
 * @Date: 2018/12/13 20:00
 * @Version 1.0
 */
public class TransferUtil {

    public static Set<String> mergeSingle(List<String> texts) {
        Set<String> combine = new HashSet<>();
        Object[] objects = texts.toArray();
        List<Integer> collect = texts.stream().map(x -> x.length() == 1 ? 1 : 0).collect(Collectors.toList());
        combine.addAll(combineSingleWithHead(texts));
        combine.addAll(combineSingleWithTail(texts));
        combine.addAll(findSingle(objects, collect));
        return combine;
    }

    public static List<String> combineSingleWithHead(List<String> texts) {
        List<String> result = new ArrayList<>();
        for (int i = 1; i < texts.size(); i++) {
            String text = texts.get(i);
            if (text.length() == 1) {
                String head = texts.get(i - 1);
                result.add(head + text);
            }
        }
        return result;
    }

    public static List<String> combineSingleWithTail(List<String> texts) {
        List<String> result = new ArrayList<>();
        for (int i = 0; i < texts.size() - 1; i++) {
            String text = texts.get(i);
            if (text.length() == 1) {
                String tail = texts.get(i + 1);
                result.add(text + tail);
            }
        }
        return result;
    }

    public static List<String> findSingle(Object[] texts, List<Integer> singles) {
        List<String> result = new ArrayList<>();

        while (sum(singles) != 0) {
            int[] ints = singles.stream().mapToInt(x -> x).toArray();
            int length = ints.length;
            List<Integer> cal = new ArrayList<>();
            cal.add(0);
            for (int i = 1; i < length; i++) {
                int temp = ints[i - 1] + ints[i];
                cal.add(temp == 2 ? 1 : 0);
                if (temp == 2) {
                    result.add(texts[i - 1] + String.valueOf(texts[i]));
                }
            }
            singles = cal;
        }
        return result;
    }

    private static int sum(List<Integer> ints) {
        return ints.stream().mapToInt(x -> (int) x).sum();
    }

    public static String accentTransfer(String pinyin) {
        String accent = pinyin;
        if (pinyin.startsWith(Constants.F)) {
            accent = pinyin.replaceFirst(Constants.F, Constants.H);
        } else if (pinyin.startsWith(Constants.H)) {
            accent = pinyin.replace(Constants.H, Constants.F);
        } else if (pinyin.startsWith(Constants.N)) {
            accent =pinyin.replaceFirst(Constants.N, Constants.L);
        } else if (pinyin.startsWith(Constants.L)) {
            accent =pinyin.replaceFirst(Constants.L, Constants.N);
        }

        return accent;
    }


    public static void main(String[] args) {
        List<String> a = new ArrayList<>();
        a.add("mergeSingle");
        a.add("b");
        a.add("cc");
        mergeSingle(a);
    }
}
